import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/avartar_view.dart';
import 'package:p_location/common/widget/norm_item.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/guest_info_obj.dart';
import 'package:p_location/home/res/res_guest_detail.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_listener.dart';

class PageGuestDetail extends StatefulWidget {

  final int id;

  PageGuestDetail(this.id);

  @override
  _PageGuestDetailState createState() => _PageGuestDetailState();
}

class _PageGuestDetailState extends State<PageGuestDetail>
    implements NetListener {
  GuestInfoObj _list;
  bool _isLoading = true;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    _requestDetail();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () {
                Navigator.pop(context);
                // 不写onPressed 默认，这个图片不能点击 且会有不可点击的样式（和 写了这个有不同的样式）
                // 如果有 onPressed 但是值是null 也会是不可点击的样式
              }),
          automaticallyImplyLeading: true,
          title: Text("外来访客"),
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
        ),
        body: _isLoading
            ? Center(
          child: CircularProgressIndicator(),
        )
            : Container(
                child: ListView(
                  children: [
                    Container(
                      width: 100,
                      height: 100,
                      alignment: AlignmentDirectional.center,
                      margin: EdgeInsets.only(right: 10),
                      child: AvatarView(
                          '${DioHttpClient.baseUrl}${_list?.pic?.downloadUrl}'),
                    ),
                    NormItem("姓名", _list.name),
                    NormItem("性别", _list.sex),
                    NormItem("身份证号", _list.carNumber),
                    NormItem("联系电话", _list.telNumber),
                    NormItem("拜访事由", _list.visitReason),
                    NormItem("备注", _list.remark),
                    NormItem("定位卡号", _list.personSn),
                  ],
                ),
              ));
  }

  _requestDetail() async {
    Map<String, dynamic> params = {};
    params["id"] = widget.id;
    await DioHttpClient.requestGet(
        context, HomeHttpUrl.GUEST_DETAIL_URL, params, this);
  }

  @override
  void onRequestSuccess(Response response) {
    if (response != null && response.data != null) {
      ResGuestDetail res = ResGuestDetail.fromJson(response.data);
      if (res != null && res.data != null) {
        _isLoading = false;
        if (res != null && res.data != null) {
          _list = res.data;
        }
        setState(() {});
      }
    }
  }

  @override
  void onRequestError(CommonRes errorRes) {}
}
